﻿using System;
using System.Net;
using System.Windows.Forms;
using SocketChat.Core.Sockets;
using SocketChat.Core.WinForm;

namespace SocketChat.Client
{
    public partial class MainForm : Form
    {
        private IPEndPoint _ServerIPEndPoint;
        private DateTime _LastLogTime;
        private TcpSocketClient _TcpSocketClient;
        private string _Name;

        public MainForm(IPEndPoint serverEndPoint, string name) {
            InitializeComponent();
            _ServerIPEndPoint = serverEndPoint;
            _Name = name;
        }

        private void MainForm_Load(object sender, EventArgs e) {
            _LastLogTime = DateTime.Now.AddSeconds(-10);
            _TcpSocketClient = new TcpSocketClient(_ServerIPEndPoint, _Name);
            _TcpSocketClient.AddMsgAction = new Action<ListBoxItem>((item) => { this.lbMsg.AddMsg(item); });

            _TcpSocketClient.Connect();
        }

        private void btnSend_Click(object sender, EventArgs e) {
            if (!string.IsNullOrEmpty(this.txtInput.Text.Trim())) {
                _TcpSocketClient.Send(new Msg { Body = this.txtInput.Text.Trim(), Header = MsgType.Msg });
                this.txtInput.Text = string.Empty;
            }
        }
    }
}
